﻿/****************************************************************************
Copyright (c) 2014-2015 凌惊雪 微信:Lingjingxue 邮箱:lingjingxue@sina.com QQ:271487457

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

using MySql.Data.MySqlClient;
using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using UtilPublic.ServerLoger;
using UtilPublic.Const;

namespace UtilPublic.Mysql
{
    /// <summary>
    /// 游戏记录系统
    /// </summary>
    public static class MysqlRecord
    {
        /// <summary>
        /// 添加记录
        /// </summary>
        /// <param name="RecType"></param>
        /// <param name="ActionSource"></param>
        /// <param name="puid"></param>
        /// <param name="args"></param>
        public static void MysqlRec(string RecType, string ActionSource, long puid = 0, params object[] args)
        {
            var sb = new StringBuilder();
            var sb1 = new StringBuilder();
            var sb2 = new StringBuilder();
            sb.Append($"insert into {PublicConst.ServerConfigId}{MysqlRecordTableName}{RecType}");
            for (int i = 0; i < args.Length; i++)
            {
                sb1.Append($", v{i + 1}");
                sb2.Append($", '{args[i]}'");
            }
            sb.Append($"(RecDate, RecTime, ActionSource, Puid{sb1}) values ");
            sb.Append($"(Now(), Now(), '{ActionSource}', '{puid}'{sb2})");
            DbMysqlSecond.AddCmd(sb.ToString());
        }
        /// <summary>
        /// 添加记录
        /// </summary>
        /// <param name="RecType"></param>
        /// <param name="ActionSource"></param>
        /// <param name="puid"></param>
        /// <param name="v0"></param>
        /// <param name="args"></param>
        public static void MysqlRecValue(string RecType, string ActionSource, long puid = 0, long v0 = 0, params object[] args)
        {
            var sb = new StringBuilder();
            var sb1 = new StringBuilder();
            var sb2 = new StringBuilder();
            sb.Append($"insert into {PublicConst.ServerConfigId}{MysqlRecordTableName}{RecType}");
            for (int i = 0; i < args.Length; i++)
            {
                sb1.Append($", v{i + 1}");
                sb2.Append($", '{args[i]}'");
            }
            sb.Append($"(RecDate, RecTime, ActionSource, Puid, v0{sb1}) values ");
            sb.Append($"(Now(), Now(), '{ActionSource}', '{puid}', '{v0}'{sb2})");
            DbMysqlSecond.AddCmd(sb.ToString());
        }
        /// <summary>
        /// 添加记录
        /// </summary>
        /// <param name="RecType"></param>
        /// <param name="ActionSource"></param>
        /// <param name="puid"></param>
        /// <param name="dt"></param>
        /// <param name="args"></param>
        public static void MysqlRecData(string RecType, string ActionSource, long puid, DateTime dt, params object[] args)
        {
            var sb = new StringBuilder();
            var sb1 = new StringBuilder();
            var sb2 = new StringBuilder();
            sb.Append($"insert into {PublicConst.ServerConfigId}{MysqlRecordTableName}{RecType}");
            for (int i = 0; i < args.Length; i++)
            {
                sb1.Append($", v{i + 1}");
                sb2.Append($", '{args[i]}'");
            }
            sb.Append($"(RecDate, RecTime, ActionSource, Puid, ExtData{sb1}) values ");
            sb.Append($"(Now(), Now(), '{ActionSource}', '{puid}', '{dt}'{sb2})");
            DbMysqlSecond.AddCmd(sb.ToString());
        }
        /// <summary>
        /// 
        /// </summary>
        public const string MysqlRecordTableName = "gamerecord";
        /// <summary>
        /// 
        /// </summary>
        /// <param name="recordtype"></param>
        /// <param name="ExtData"></param>
        public static void MysqlCreateTableGameRecord(string recordtype, bool ExtData)
        {
            var sb = new StringBuilder();
            sb.Append($"CREATE TABLE IF NOT EXISTS {PublicConst.ServerConfigId}{MysqlRecordTableName}{recordtype}(");
            sb.Append($"`Id` bigint(31) NOT NULL AUTO_INCREMENT,");
            sb.Append($"`RecDate` date NOT NULL,");
            sb.Append($"`RecTime` datetime NOT NULL,");
            sb.Append($"`ActionSource` varchar(255) NOT NULL,");
            sb.Append($"`Puid` bigint(31) NOT NULL,");
            sb.Append($"`v0` bigint(31) DEFAULT '0',");
            sb.Append($"`v1` text,");
            sb.Append($"`v2` text,");
            sb.Append($"`v3` text,");
            sb.Append($"`v4` text,");
            sb.Append($"`v5` text,");
            sb.Append($"`v6` text,");
            sb.Append($"`v7` text,");
            sb.Append($"`v8` text,");
            sb.Append($"`v9` text,");
            if (ExtData)
            {
                sb.Append($"`ExtData` date NOT NULL,");
            }
            sb.Append($"PRIMARY KEY (`Id`)");
            sb.Append($") ENGINE = InnoDB DEFAULT CHARSET = utf8;");
            DbMysqlSecond.AddCmd(sb.ToString());
        }
    }


}